﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;

namespace FACUI.BasicClass
{
    /// <summary>
    /// 信号机开关量
    /// </summary>
    public class XhjKgl
    {
        public string green = "";
        public string name = "";
        public string red = "";
        public string yellow = "";
    }
    /*************************************/
    /// <summary>
    /// 开关量
    /// </summary>
    public class Kgl
    {
        public string addType = "";
        public int AdNo = -1;
        public int delMark = 0;
        public int FjAd = -1;
        public int FjNo = -1;
        public int id = -1;
        public bool IsReverse = true;
        public string KglName = "";
        public string Note = "";
        public string SubKgl1Name = "";
        public string SubKgl2Name = "";
        public string SubKglOp = "";
        public string SubType = "空";

        public Kgl(string KglName)
        {
            this.KglName = KglName;
        }

        public static KglCollection FromCfgNode(CfgNode kglNode, bool isLjKgl)
        {
            if (kglNode == null)
            {
                return null;
            }
            if (isLjKgl)
            {
                kglNode = kglNode.GetSubNode("智能分析逻辑开关量");
                if (kglNode == null)
                {
                    return null;
                }
            }
            else
            {
                kglNode = kglNode.GetSubNode("实际开关量");
                if (kglNode == null)
                {
                    return null;
                }
            }
            KglCollection kgls = new KglCollection();
            foreach (CfgNode node in kglNode.GetSubNodeList())
            {
                Kgl kgl = new Kgl("")
                {
                    KglName = node.GetValue("开关量名称"),
                    id = Util.ToInt(node.GetValue("ID"), -1)
                };
                if (isLjKgl)
                {
                    kgl.SubKgl1Name = node.GetValue("子开关量1");
                    kgl.SubKgl2Name = node.GetValue("子开关量2");
                    kgl.SubKglOp = node.GetValue("逻辑关系");
                    kgl.delMark = Util.ToInt(node.GetValue("只读"), 0);
                    kgl.Note = node.GetValue("备注");
                }
                else
                {
                    kgl.IsReverse = node.GetValue("是否取反") == "是";
                    kgl.AdNo = Util.ToInt(node.GetValue("AD号"), -1);
                    kgl.FjNo = Util.ToInt(node.GetValue("分机号"), -1);
                    kgl.FjAd = Util.ToInt(node.GetValue("分机内AD号"), -1);
                    kgl.Note = node.GetValue("备注");
                    kgl.SubType = node.GetValue("开关量类型");
                    kgl.addType = node["添加类型"];
                }
                kgls.Add(kgl);
            }
            return kgls;
        }

        public static string[] GetPtDcDfb(Dictionary<string, DcKgl> zctDckglDic, CfgNode node, string s)
        {
            if ((zctDckglDic == null) || (node == null))
            {
                return null;
            }
            string[] strArray = new string[] { "", "", "" };
            string dbName = "";
            foreach (KeyValuePair<string, DcKgl> pair in zctDckglDic)
            {
                if (s == "-L")
                {
                    dbName = pair.Value.dbName;
                }
                else if (s == "-U")
                {
                    dbName = pair.Value.fbName;
                }
                if (pair.Key == node["设备名称"])
                {
                    strArray[0] = dbName;
                    return strArray;
                }
                if (!node["设备名称"].Contains("/"))
                {
                    if (Util.CompareDcName(pair.Key, node["设备名称"]))
                    {
                        strArray[0] = dbName;
                        return strArray;
                    }
                }
                else
                {
                    string[] strArray2 = node["设备名称"].Split(new char[] { '/' });
                    string key = pair.Key;
                    string str3 = "";
                    if (pair.Key.Contains("/"))
                    {
                        key = pair.Key.Split(new char[] { '/' })[0];
                        str3 = pair.Key.Split(new char[] { '/' })[1];
                    }
                    try
                    {
                        if ((Convert.ToInt32(strArray2[1]) - Convert.ToInt32(strArray2[0])) >= 2)
                        {
                            if ((strArray2[0] == key) || (strArray2[0] == str3))
                            {
                                strArray[0] = dbName;
                            }
                            if ((strArray2[1] == key) || (strArray2[1] == str3))
                            {
                                strArray[1] = dbName;
                            }
                            if ((Convert.ToInt32(strArray2[1]) - Convert.ToInt32(strArray2[0])) == 4)
                            {
                                if ((strArray2[1] == key) || (strArray2[1] == str3))
                                {
                                    strArray[2] = dbName;
                                }
                                int num = Convert.ToInt32(strArray2[0]) + 2;
                                if ((num.ToString() == key) || ((num = Convert.ToInt32(strArray2[0]) + 2).ToString() == str3))
                                {
                                    strArray[1] = dbName;
                                }
                            }
                        }
                    }
                    catch
                    {
                    }
                }
            }
            return strArray;
        }

        public static string[] GetTsDcDfb(CfgNode node, KglCollection kglList, string s)
        {
            string[] strArray = new string[] { "", "", "" };
            if ((kglList != null) && (node != null))
            {
                foreach (Kgl kgl in (IEnumerable)kglList)
                {
                    if (kgl.KglName.EndsWith(s) && (kgl.FjNo != 0x10f))
                    {
                        if (kgl.KglName == (node["设备名称"] + s))
                        {
                            strArray[0] = kgl.KglName;
                            return strArray;
                        }
                        if (kgl.KglName.Contains("-"))
                        {
                            int index = kgl.KglName.IndexOf('-');
                            if ((index != (kgl.KglName.Length - 1)) && (kgl.KglName.Remove(index, 1) == (node["设备名称"] + s)))
                            {
                                strArray[0] = kgl.KglName;
                                return strArray;
                            }
                        }
                    }
                }
            }
            return strArray;
        }

        public static Dictionary<string, DcKgl> GetZctDcKgl(StationCfg stacfg)
        {
            if (stacfg == null)
            {
                return null;
            }
            Dictionary<string, DcKgl> dictionary = new Dictionary<string, DcKgl>();
            CfgNode mapInfoCfgNode = stacfg.GetMapInfoCfgNode();
            if (mapInfoCfgNode == null)
            {
                return null;
            }
            CfgNode subNode = mapInfoCfgNode.GetSubNode("道岔");
            if (subNode == null)
            {
                return null;
            }
            int num = 0;
            CfgNode node3 = mapInfoCfgNode.GetSubNode("概述");
            if (node3 != null)
            {
                num = Convert.ToInt32(node3["道岔数目"]);
            }
            string str = "";
            DcKgl kgl = null;
            for (int i = 1; i <= num; i++)
            {
                kgl = new DcKgl();
                str = subNode[i.ToString()];
                try
                {
                    kgl.name = str.Split(new char[] { ';' })[0].Split(new char[] { ',' })[1];
                    kgl.dbName = str.Split(new char[] { ';' })[0].Split(new char[] { ',' })[2];
                    kgl.fbName = str.Split(new char[] { ';' })[2].Split(new char[] { ',' })[1];
                    kgl.zyName = str.Split(new char[] { ';' })[4].Split(new char[] { ',' })[1];
                    kgl.sbName = str.Split(new char[] { ';' })[6].Split(new char[] { ',' })[1];
                    if (!dictionary.ContainsKey(kgl.name))
                    {
                        dictionary.Add(kgl.name, kgl);
                    }
                }
                catch
                {
                }
            }
            return dictionary;
        }

        public static Dictionary<string, GdKgl> GetZctWcqdKgl(StationCfg stacfg)
        {
            if (stacfg == null)
            {
                return null;
            }
            Dictionary<string, GdKgl> dictionary = new Dictionary<string, GdKgl>();
            CfgNode mapInfoCfgNode = stacfg.GetMapInfoCfgNode();
            if (mapInfoCfgNode == null)
            {
                return null;
            }
            CfgNode subNode = mapInfoCfgNode.GetSubNode("无岔区段");
            CfgNode node3 = mapInfoCfgNode.GetSubNode("概述");
            CfgNode node4 = mapInfoCfgNode.GetSubNode("详述");
            if (((subNode == null) || (node3 == null)) || (node4 == null))
            {
                return null;
            }
            int num = Convert.ToInt32(node3["无岔区段数目"]);
            int num2 = Convert.ToInt32(node3["控件总数"]);
            Dictionary<string, int> dictionary2 = new Dictionary<string, int>();
            string str = "";
            int num3 = 0;
            int num4 = 1;
            while (num4 <= num2)
            {
                str = node4["控件描述" + num4.ToString()];
                if (str.Split(new char[] { ',' })[1] == "sbWCQD")
                {
                    num3++;
                    if (!dictionary2.ContainsKey(str.Split(new char[] { ',' })[0]))
                    {
                        dictionary2.Add(str.Split(new char[] { ',' })[0], num3);
                    }
                }
                num4++;
            }
            string str2 = "";
            GdKgl kgl = null;
            if (dictionary2 == null)
            {
                return null;
            }
            foreach (KeyValuePair<string, int> pair in dictionary2)
            {
                for (num4 = 1; num4 <= num; num4++)
                {
                    if (num4 == pair.Value)
                    {
                        kgl = new GdKgl();
                        str2 = subNode[num4.ToString()];
                        kgl.name = pair.Key;
                        kgl.zyName = str2.Split(new char[] { ';' })[0].Split(new char[] { ',' })[1];
                        kgl.sbName = str2.Split(new char[] { ';' })[2].Split(new char[] { ',' })[1];
                        try
                        {
                            dictionary.Add(kgl.name, kgl);
                        }
                        catch (Exception exception)
                        {
                            exception.ToString();
                        }
                    }
                }
            }
            return dictionary;
        }

        public static int IsNumeric(string str)
        {
            if ((str != null) && Regex.IsMatch(str, @"^\d+$"))
            {
                return int.Parse(str);
            }
            return -1;
        }

        public static void RemoveAddTypeKgl(KglCollection kgllist, string addtype)
        {
            int num = 0;
            int count = kgllist.Count;
            while (true)
            {
                num = 0;
                count = kgllist.Count;
                while (num < count)
                {
                    Kgl kgl = kgllist[num];
                    if (kgl.addType == addtype)
                    {
                        kgllist.Remove(kgl);
                        break;
                    }
                    num++;
                }
                if (num == count)
                {
                    return;
                }
            }
        }

        public static string[] SetDcCAKgl(CfgNode node, KglCollection kglList, string s)
        {
            if ((node == null) || (kglList == null))
            {
                return null;
            }
            string[] strArray = new string[] { "", "", "" };
            foreach (Kgl kgl in (IEnumerable)kglList)
            {
                if (kgl.KglName.EndsWith(s))
                {
                    if (!node["设备名称"].Contains("/"))
                    {
                        string str = "";
                        if (node["设备名称"].Contains("J") || node["设备名称"].Contains("X"))
                        {
                            int startIndex = -1;
                            if (!node["设备名称"].Contains("-"))
                            {
                                if (node["设备名称"].Contains("J"))
                                {
                                    startIndex = node["设备名称"].LastIndexOf("J");
                                }
                                else if (node["设备名称"].Contains("X"))
                                {
                                    startIndex = node["设备名称"].LastIndexOf("X");
                                }
                            }
                            else
                            {
                                startIndex = node["设备名称"].LastIndexOf("-");
                            }
                            str = node["设备名称"].Remove(startIndex);
                        }
                        else
                        {
                            str = node["设备名称"];
                        }
                        if (kgl.KglName == (str + s))
                        {
                            strArray[0] = kgl.KglName;
                            return strArray;
                        }
                        if (kgl.KglName.Contains("/") && ((kgl.KglName.Replace(s, "").Split(new char[] { '/' })[0] == str) || (kgl.KglName.Replace(s, "").Split(new char[] { '/' })[1] == str)))
                        {
                            strArray[0] = kgl.KglName;
                            return strArray;
                        }
                    }
                    else
                    {
                        string[] strArray2 = node["设备名称"].Split(new char[] { '/' });
                        string str2 = kgl.KglName.Replace(s, "");
                        string str3 = "";
                        if (kgl.KglName.Contains("/"))
                        {
                            str2 = kgl.KglName.Replace(s, "").Split(new char[] { '/' })[0];
                            str3 = kgl.KglName.Replace(s, "").Split(new char[] { '/' })[1];
                        }
                        try
                        {
                            if ((Convert.ToInt32(strArray2[1]) - Convert.ToInt32(strArray2[0])) >= 2)
                            {
                                if ((strArray2[0] == str2) || (strArray2[0] == str3))
                                {
                                    strArray[0] = kgl.KglName;
                                }
                                if ((strArray2[1] == str2) || (strArray2[1] == str3))
                                {
                                    strArray[1] = kgl.KglName;
                                }
                                if ((Convert.ToInt32(strArray2[1]) - Convert.ToInt32(strArray2[0])) == 4)
                                {
                                    if ((strArray2[1] == str2) || (strArray2[1] == str3))
                                    {
                                        strArray[2] = kgl.KglName;
                                    }
                                    int num2 = Convert.ToInt32(strArray2[0]) + 2;
                                    if ((num2.ToString() == str2) || ((num2 = Convert.ToInt32(strArray2[0]) + 2).ToString() == str3))
                                    {
                                        strArray[1] = kgl.KglName;
                                    }
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }
            return strArray;
        }

        public static void SetDcQkKgl(StationCfg staCfg, KglCollection kglList)
        {
            if ((staCfg != null) && (kglList != null))
            {
                CfgNode devTypeNode = staCfg.GetDevTypeNode("道岔");
                if (devTypeNode != null)
                {
                    CfgNode nodeDirect = devTypeNode.GetNodeDirect(@"属性列表\道岔缺口开始AD号");
                    if (nodeDirect != null)
                    {
                        foreach (CfgNode node3 in nodeDirect.GetSubNodeList())
                        {
                            foreach (Kgl kgl in (IEnumerable)kglList)
                            {
                                if ((kgl.SubType == "道岔缺口报警开关量") && ((kgl.Note == (node3["设备名称"] + "道岔缺口开始AD号")) || kgl.KglName.Contains("_空路_1")))
                                {
                                    node3["开始AD号"] = kgl.AdNo.ToString();
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }

        public static string[] SetDcZyOrSbKgl(Dictionary<string, DcKgl> zctDckglDic, CfgNode node, string s)
        {
            string[] strArray = new string[] { "", "", "" };
            if ((node != null) && (zctDckglDic != null))
            {
                string zyName = "";
                foreach (KeyValuePair<string, DcKgl> pair in zctDckglDic)
                {
                    if (s == "H")
                    {
                        zyName = pair.Value.zyName;
                    }
                    else if (s == "B")
                    {
                        zyName = pair.Value.sbName;
                    }
                    if (pair.Key == node["设备名称"])
                    {
                        strArray[0] = zyName;
                        return strArray;
                    }
                    if (!node["设备名称"].Contains("/"))
                    {
                        if (Util.CompareDcName(pair.Key, node["设备名称"]))
                        {
                            strArray[0] = zyName;
                            return strArray;
                        }
                    }
                    else
                    {
                        string[] strArray2 = node["设备名称"].Split(new char[] { '/' });
                        string key = pair.Key;
                        string str3 = "";
                        string str4 = strArray2[0].Replace("(", "").Replace(")", "");
                        string str5 = strArray2[1].Replace("(", "").Replace(")", "");
                        if (pair.Key.Contains("/"))
                        {
                            key = pair.Key.Split(new char[] { '/' })[0];
                            str3 = pair.Key.Split(new char[] { '/' })[1];
                        }
                        try
                        {
                            if ((Convert.ToInt32(str5) - Convert.ToInt32(str4)) >= 2)
                            {
                                if ((str4 == key) || (str4 == str3))
                                {
                                    strArray[0] = zyName;
                                }
                                if ((str5 == key) || (str5 == str3))
                                {
                                    strArray[1] = zyName;
                                }
                                if ((Convert.ToInt32(str5) - Convert.ToInt32(str4)) == 4)
                                {
                                    if ((str5 == key) || (str5 == str3))
                                    {
                                        strArray[2] = zyName;
                                    }
                                    int num = Convert.ToInt32(str4) + 2;
                                    if ((num.ToString() == key) || ((num = Convert.ToInt32(str4) + 2).ToString() == str3))
                                    {
                                        strArray[1] = zyName;
                                    }
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }
            return strArray;
        }

        public static void SetDevKglRs(StationCfg staCfg, KglCollection kglList)
        {
            if ((staCfg != null) && (kglList != null))
            {
                foreach (Kgl kgl in (IEnumerable)kglList)
                {
                    if (kgl.KglName == "RS-H")
                    {
                        SetRs(staCfg.GetDevTypeNode("道岔"));
                        SetRs(staCfg.GetDevTypeNode("信号机"));
                        SetRs(staCfg.GetDevTypeNode("轨道"));
                        break;
                    }
                }
            }
        }

        private static void SetDevkglZDorZF(StationCfg staCfg, KglCollection kglList)
        {
            if ((staCfg != null) && (kglList != null))
            {
                string kglSZ = "";
                string str3 = "";
                string kglXZ = "";
                string str5 = "";
                foreach (Kgl kgl in (IEnumerable)kglList)
                {
                    string str = kgl.KglName.ToUpper().Trim();
                    if ((((kglXZ != "") && (kglSZ != "")) && (str5 != "")) && (str3 != ""))
                    {
                        break;
                    }
                    if (str.Length < 4)
                    {
                        if (str.Contains("ZD") && !str.Contains("Y"))
                        {
                            if (str.StartsWith("SZD"))
                            {
                                kglSZ = str;
                            }
                            else if (str.StartsWith("XZD"))
                            {
                                kglXZ = str;
                            }
                            else if (str.StartsWith("ZD"))
                            {
                                kglXZ = str;
                                kglSZ = str;
                            }
                        }
                        else if (str.Contains("ZF") && !str.Contains("Y"))
                        {
                            if (str.StartsWith("SZF"))
                            {
                                str3 = str;
                            }
                            else if (str.StartsWith("XZF"))
                            {
                                str5 = str;
                            }
                            else if (str.StartsWith("ZF"))
                            {
                                str5 = str;
                                str3 = str;
                            }
                        }
                    }
                }
                CfgNode devTypeNode = staCfg.GetDevTypeNode("道岔");
                if (devTypeNode != null)
                {
                    SetDevkglZDorZF(devTypeNode.GetNodeDirect(@"属性列表\ZD"), kglSZ, kglXZ);
                    SetDevkglZDorZF(devTypeNode.GetNodeDirect(@"属性列表\ZF"), str3, str5);
                }
            }
        }

        private static void SetDevkglZDorZF(CfgNode propnode, string kglSZ, string kglXZ)
        {
            if ((propnode != null) && ((kglSZ != null) || (kglXZ != null)))
            {
                try
                {
                    foreach (CfgNode node in propnode.GetSubNodeList())
                    {
                        if (node["开关量名称"] != "")
                        {
                            return;
                        }
                        string val = "";
                        string str = node["设备名称"];
                        if (str.Contains("/"))
                        {
                            str = str.Split(new char[] { '/' })[0];
                        }
                        for (int i = 0; i < str.Length; i++)
                        {
                            if ((str[i] < '0') || (str[i] > '9'))
                            {
                                break;
                            }
                            val = val + str[i];
                        }
                        if (val != "")
                        {
                            if ((Util.ToInt(val, 0) % 2) == 0)
                            {
                                node["开关量名称"] = kglSZ;
                            }
                            else
                            {
                                node["开关量名称"] = kglXZ;
                            }
                        }
                    }
                }
                catch
                {
                }
            }
        }

        public static void SetDevlglZQorZR(StationCfg staCfg, KglCollection kglList)
        {
            if ((staCfg != null) && (kglList != null))
            {
                string kglSZ = "";
                string str3 = "";
                string kglXZ = "";
                string str5 = "";
                foreach (Kgl kgl in (IEnumerable)kglList)
                {
                    string str = kgl.KglName.ToUpper().Trim();
                    if ((((kglXZ != "") && (kglSZ != "")) && (str5 != "")) && (str3 != ""))
                    {
                        break;
                    }
                    if (str.Length < 5)
                    {
                        if (str.Contains("ZQ"))
                        {
                            if (str.StartsWith("SZQ"))
                            {
                                kglSZ = str;
                            }
                            if (str.StartsWith("XZQ"))
                            {
                                kglXZ = str;
                            }
                            else if (str.StartsWith("ZQ"))
                            {
                                kglSZ = str;
                                kglXZ = str;
                            }
                        }
                        else if (str.Contains("ZR"))
                        {
                            if (str.StartsWith("SZR"))
                            {
                                str3 = str;
                            }
                            if (str.StartsWith("XZR"))
                            {
                                str5 = str;
                            }
                            else if (str.StartsWith("ZR"))
                            {
                                str3 = str;
                                str5 = str;
                            }
                        }
                    }
                }
                CfgNode devTypeNode = staCfg.GetDevTypeNode("信号机");
                if (devTypeNode != null)
                {
                    SetDevlglZQorZR(devTypeNode.GetNodeDirect(@"属性列表\ZQ"), kglSZ, kglXZ);
                    SetDevlglZQorZR(devTypeNode.GetNodeDirect(@"属性列表\ZR"), str3, str5);
                }
            }
        }

        public static void SetDevlglZQorZR(CfgNode propnode, string kglSZ, string kglXZ)
        {
            if (propnode != null)
            {
                foreach (CfgNode node in propnode.GetSubNodeList())
                {
                    if (node["开关量名称"] != "")
                    {
                        continue;
                    }
                    string str = node["设备名称"].ToUpper();
                    if (str[0] == 'X')
                    {
                        node["开关量名称"] = kglSZ;
                        continue;
                    }
                    if (str[0] == 'S')
                    {
                        node["开关量名称"] = kglXZ;
                        continue;
                    }
                    if (str[0] == 'D')
                    {
                        string val = "";
                        for (int i = 1; i < str.Length; i++)
                        {
                            if ((str[i] < '0') || (str[i] > '9'))
                            {
                                break;
                            }
                            val = val + str[i];
                        }
                        if (val != "")
                        {
                            if ((Util.ToInt(val, 0) % 2) == 0)
                            {
                                node["开关量名称"] = kglSZ;
                            }
                            else
                            {
                                node["开关量名称"] = kglXZ;
                            }
                        }
                    }
                }
            }
        }

        public static void SetGDZYKgl(StationCfg staCfg, KglCollection kglList)
        {
            if ((staCfg != null) && (kglList != null))
            {
                CfgNode devTypeNode = staCfg.GetDevTypeNode("轨道");
                if (devTypeNode != null)
                {
                    CfgNode nodeDirect = devTypeNode.GetNodeDirect(@"属性列表\占用开关量");
                    if (nodeDirect != null)
                    {
                        foreach (CfgNode node3 in nodeDirect.GetSubNodeList())
                        {
                            if ((node3["开关量名称"] != "") && (node3["开关量名称1"] != ""))
                            {
                                break;
                            }
                            foreach (Kgl kgl in (IEnumerable)kglList)
                            {
                                if (kgl.KglName == (node3["设备名称"] + "J"))
                                {
                                    node3["开关量名称"] = kgl.KglName.ToString();
                                    break;
                                }
                                if ((((kgl.KglName == (node3["设备名称"].Replace("DG", "") + "-H")) || (kgl.KglName == (node3["设备名称"].Replace("DG", "") + "-QH"))) || (kgl.KglName == (node3["设备名称"] + "-H"))) || (kgl.KglName == (node3["设备名称"] + "-3H")))
                                {
                                    node3["开关量名称1"] = kgl.KglName.ToString();
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }

        public static string SetGDZYSBKgl(Dictionary<string, GdKgl> zctWcqdKglDic, CfgNode node, KglCollection kglList, string s)
        {
            if ((node == null) || (kglList == null))
            {
                return "";
            }
            string str = "";
            if (node["设备名称"].Contains("DG"))
            {
                foreach (Kgl kgl in (IEnumerable)kglList)
                {
                    if (kgl.KglName.EndsWith(s) && (((((kgl.KglName == (node["设备名称"] + s)) || (kgl.KglName == (node["设备名称"].Replace("DG", "") + s))) || ((kgl.KglName.Replace(s, "") == node["设备名称"].Replace("DG1", "")) || (kgl.KglName.Replace(s, "") == node["设备名称"].Replace("DG2", "")))) || (kgl.KglName.Replace(s, "") == node["设备名称"].Replace("DG3", ""))) || (kgl.KglName.Replace(s, "") == node["设备名称"].Split(new char[] { '-' })[0])))
                    {
                        return kgl.KglName;
                    }
                }
                return str;
            }
            if (zctWcqdKglDic == null)
            {
                return "";
            }
            string zyName = "";
            foreach (KeyValuePair<string, GdKgl> pair in zctWcqdKglDic)
            {
                if (s == "-QH")
                {
                    zyName = pair.Value.zyName;
                }
                else if (s == "-QB")
                {
                    zyName = pair.Value.sbName;
                }
                if (pair.Key == node["设备名称"])
                {
                    return zyName;
                }
            }
            return str;
        }

        public static string SetJcDcDFBKgl(CfgNode node, KglCollection kglList, string s)
        {
            if ((node == null) || (kglList == null))
            {
                return "";
            }
            string kglName = "";
            string str2 = "";
            foreach (Kgl kgl in (IEnumerable)kglList)
            {
                if (kgl.KglName.EndsWith(s) && (kgl.FjNo != 0x10f))
                {
                    if ((kgl.KglName == (node["设备名称"] + s)) || (kgl.KglName == (node["设备名称"] + "-A" + s)))
                    {
                        return kgl.KglName;
                    }
                    if (!node["设备名称"].Contains("/"))
                    {
                        if (kgl.KglName.Contains("X") || kgl.KglName.Contains("J"))
                        {
                            if (kgl.KglName.StartsWith(node["设备名称"]) && (kgl.KglName.Contains("-") || kgl.KglName.Contains("_")))
                            {
                                int index = kgl.KglName.IndexOf('-');
                                if (index == -1)
                                {
                                    index = kgl.KglName.IndexOf('_');
                                }
                                if (index != (kgl.KglName.Length - 1))
                                {
                                    str2 = node["设备名称"] + s;
                                    if (kgl.KglName == str2)
                                    {
                                        return kgl.KglName;
                                    }
                                    str2 = node["设备名称"] + "A" + s;
                                    if (kgl.KglName == str2)
                                    {
                                        return kgl.KglName;
                                    }
                                }
                            }
                        }
                        else if (kgl.KglName.Contains("/"))
                        {
                            if (((kgl.KglName == (node["设备名称"] + s)) || (kgl.KglName.Replace(s, "").Split(new char[] { '/' })[0] == node["设备名称"])) || (kgl.KglName.Replace(s, "").Split(new char[] { '/' })[1] == node["设备名称"]))
                            {
                                return kgl.KglName;
                            }
                        }
                        else if (kgl.KglName.StartsWith(node["设备名称"]) && kgl.KglName.Contains(s))
                        {
                            return kgl.KglName;
                        }
                    }
                    else
                    {
                        string[] strArray = node["设备名称"].Split(new char[] { '/' });
                        foreach (string str3 in strArray)
                        {
                            if (kgl.KglName.StartsWith(node["设备名称"]) && kgl.KglName.Contains(s))
                            {
                                kglName = kgl.KglName;
                                break;
                            }
                        }
                    }
                }
            }
            return kglName;
        }

        public static void SetRs(CfgNode node)
        {
            if (node != null)
            {
                CfgNode nodeDirect = node.GetNodeDirect(@"属性列表\RS");
                if (nodeDirect != null)
                {
                    try
                    {
                        foreach (CfgNode node3 in nodeDirect.GetSubNodeList())
                        {
                            if (node.NodeName != "道岔")
                            {
                                if (node3["开关量名称"] != "")
                                {
                                    return;
                                }
                                node3["开关量名称"] = "RS-H";
                            }
                            else if (!node3["设备名称"].Contains("/"))
                            {
                                if (node3["开关量名称1"] != "")
                                {
                                    return;
                                }
                                node3["开关量名称1"] = "RS-H";
                            }
                            else
                            {
                                if (node3["开关量名称1"] == "")
                                {
                                    node3["开关量名称1"] = "RS-H";
                                }
                                if (node3["开关量名称2"] == "")
                                {
                                    node3["开关量名称2"] = "RS-H";
                                }
                                if (((Convert.ToInt32(node3["设备名称"].Split(new char[] { '/' })[1]) - Convert.ToInt32(node3["设备名称"].Split(new char[] { '/' })[0])) >= 4) && (node3["开关量名称3"] == ""))
                                {
                                    node3["开关量名称3"] = "RS-H";
                                }
                            }
                        }
                    }
                    catch
                    {
                    }
                }
            }
        }

        public static string SetXhjDwKgl(CfgNode node, KglCollection kglList, string s)
        {
            if ((node == null) || (kglList == null))
            {
                return "";
            }
            string kglName = "";
            foreach (Kgl kgl in (IEnumerable)kglList)
            {
                if (s == "-B")
                {
                    if (((kgl.KglName.EndsWith(s) || kgl.KglName.EndsWith("-YB")) && (kgl.FjNo == 0x10f)) && (((node["设备名称"] + s) == kgl.KglName) || ((node["设备名称"] + "-YB") == kgl.KglName)))
                    {
                        kglName = kgl.KglName;
                    }
                }
                else if (kgl.KglName.EndsWith(s) && ((node["设备名称"] + s) == kgl.KglName))
                {
                    if ((s == "-U") || (s == "-L"))
                    {
                        if (!(node["设备名称"].Contains("S") || node["设备名称"].Contains("X")))
                        {
                            kglName = kgl.KglName;
                        }
                        else if (kgl.FjNo == 0x10f)
                        {
                            kglName = kgl.KglName;
                        }
                    }
                    else if (s == "-H")
                    {
                        if (!(node["设备名称"].Contains("S") || node["设备名称"].Contains("X")))
                        {
                            kglName = kgl.KglName;
                        }
                        else if ((kgl.FjNo == 0x10f) || (kgl.AdNo == -1))
                        {
                            kglName = kgl.KglName;
                        }
                    }
                    else
                    {
                        kglName = kgl.KglName;
                    }
                }
            }
            return kglName;
        }

        public static void SetXhjLgoicRed(StationCfg staCfg, KglCollection kglList)
        {
            if (staCfg != null)
            {
                Dictionary<string, XhjKgl> dictionary = new Dictionary<string, XhjKgl>();
                CfgNode devTypeNode = staCfg.GetDevTypeNode("信号机");
                if (devTypeNode != null)
                {
                    CfgNode nodeDirect = devTypeNode.GetNodeDirect(@"属性列表\X-H");
                    CfgNode node3 = devTypeNode.GetNodeDirect(@"属性列表\X-U");
                    CfgNode node4 = devTypeNode.GetNodeDirect(@"属性列表\X-L");
                    if (((nodeDirect != null) && (node3 != null)) && (node4 != null))
                    {
                        CfgNode subNode = staCfg.GetKglCfgNode().GetSubNode("智能分析逻辑开关量");
                        if (subNode != null)
                        {
                            int num = Convert.ToInt32(subNode["开关量数目"]);
                            int maxID = DeviceInfo.GetMaxID(kglList);
                            foreach (CfgNode node6 in nodeDirect.GetSubNodeList())
                            {
                                XhjKgl kgl = new XhjKgl
                                {
                                    name = node6["设备名称"],
                                    red = node6["开关量名称"]
                                };
                                foreach (CfgNode node7 in node3.GetSubNodeList())
                                {
                                    if (node7["设备名称"] == kgl.name)
                                    {
                                        kgl.yellow = node7["开关量名称"];
                                        break;
                                    }
                                }
                                foreach (CfgNode node8 in node4.GetSubNodeList())
                                {
                                    if (node8["设备名称"] == kgl.name)
                                    {
                                        kgl.green = node8["开关量名称"];
                                        break;
                                    }
                                }
                                dictionary.Add(kgl.name, kgl);
                            }
                            foreach (KeyValuePair<string, XhjKgl> pair in dictionary)
                            {
                                CfgNode node9 = new CfgNode("");
                                if ((pair.Value.red == "") && (pair.Value.green != ""))
                                {
                                    int num3 = ++num;
                                    node9.NodeName = "开关量" + num3.ToString();
                                    node9["开关量名称"] = pair.Key + "-H";
                                    node9["子开关量1"] = pair.Value.green;
                                    node9["逻辑关系"] = "或后取反";
                                    node9["AD号"] = "-1";
                                    node9["ID"] = (++maxID).ToString();
                                    if (pair.Value.yellow != "")
                                    {
                                        node9["子开关量2"] = pair.Value.yellow;
                                    }
                                    else
                                    {
                                        node9["子开关量2"] = pair.Value.green;
                                    }
                                    subNode.AddSubNode(node9);
                                }
                            }
                            subNode["开关量数目"] = num.ToString();
                            foreach (CfgNode node6 in nodeDirect.GetSubNodeList())
                            {
                                if ((node6["开关量名称"] == "") && (dictionary[node6["设备名称"]].green != ""))
                                {
                                    node6["开关量名称"] = node6["设备名称"] + "-H";
                                }
                            }
                        }
                    }
                }
            }
        }

        public static string[] SetZnfxDcDFBKgl(Dictionary<string, DcKgl> zctDckglDic, CfgNode node, KglCollection kglList, string s)
        {
            if ((node == null) || (kglList == null))
            {
                return null;
            }
            string[] strArray = new string[] { "", "", "" };
            if (node["设备名称"].Contains("X") || node["设备名称"].Contains("J"))
            {
                strArray = GetTsDcDfb(node, kglList, s);
            }
            else
            {
                strArray = GetPtDcDfb(zctDckglDic, node, s);
            }
            return strArray;
        }

        public static void ToCfgNode(CfgNode kglNode, KglCollection kglList, bool isLjKgl)
        {
            if (kglNode != null)
            {
                if (isLjKgl)
                {
                    kglNode = kglNode.GetSubNode("智能分析逻辑开关量", true);
                }
                else
                {
                    kglNode = kglNode.GetSubNode("实际开关量", true);
                }
                if (kglList == null)
                {
                    kglNode.AddValue("开关量数目", "0");
                }
                else
                {
                    kglNode.AddValue("开关量数目", kglList.Count.ToString());
                    int num = 0;
                    kglNode.ClearSubNodes();
                    foreach (Kgl kgl in (IEnumerable)kglList)
                    {
                        int num2 = ++num;
                        CfgNode subNode = new CfgNode("开关量" + num2.ToString());
                        subNode["开关量名称"] = kgl.KglName;
                        if (isLjKgl)
                        {
                            subNode["AD号"] = kgl.AdNo.ToString();
                            subNode["子开关量1"] = kgl.SubKgl1Name;
                            subNode["子开关量2"] = kgl.SubKgl2Name;
                            subNode["逻辑关系"] = kgl.SubKglOp;
                            subNode["只读"] = kgl.delMark.ToString();
                            subNode["备注"] = kgl.Note;
                        }
                        else
                        {
                            subNode["是否取反"] = kgl.IsReverse ? "是" : "否";
                            subNode["AD号"] = kgl.AdNo.ToString();
                            subNode["分机号"] = kgl.FjNo.ToString();
                            subNode["分机内AD号"] = kgl.FjAd.ToString();
                            subNode["开关量类型"] = kgl.SubType;
                            subNode["备注"] = kgl.Note;
                        }
                        subNode["ID"] = kgl.id.ToString();
                        kglNode.AddSubNode(subNode);
                    }
                }
            }
        }
    }
}

